package com.ccl.iot.views;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

/**
 * 创建者     王浩
 * 创建时间   2017/4/5 11:26
 * 描述	      ${TODO}
 */
public class VisualOrientView extends View {

    private Paint mBgPaint, mPaint1, mPaint2;
    private RectF mRectF;

    private int mStartAngles1, mStartAngles2;
    private int mSweepAngle1, mSweepAngle2;

    private boolean isArc1Add = true;
    private boolean isArc2Add = false;

    public VisualOrientView(Context context) {
        this(context, null);
    }

    public VisualOrientView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public VisualOrientView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mBgPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mBgPaint.setColor(Color.RED);
        mBgPaint.setStyle(Paint.Style.FILL);

        mPaint1 = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint1.setColor(Color.parseColor("#88dddddd"));
        mPaint1.setStyle(Paint.Style.FILL);

        mPaint2 = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint2.setColor(Color.parseColor("#88eeeeee"));
        mPaint2.setStyle(Paint.Style.FILL);

        mStartAngles1 = 290;
        mStartAngles2 = 300;

        mSweepAngle1 = 50;
        mSweepAngle2 = 30;
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mRectF = new RectF(0 - w, 0, w, h+h);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawArc(mRectF, 270, 90, true, mBgPaint);
        canvas.drawArc(mRectF, mStartAngles1, mSweepAngle1, true, mPaint1);
        canvas.drawArc(mRectF, mStartAngles2, mSweepAngle2, true, mPaint2);

        postDelayed(drawTask, 100);
    }

    private Runnable drawTask = new Runnable() {
        @Override
        public void run() {
            if(isArc1Add){
                mStartAngles1 -= 5;
                mSweepAngle1 += 10;
                if(mStartAngles1 <= 270){
                    isArc1Add = false;
                }
            }else{
                mStartAngles1 += 5;
                mSweepAngle1 -= 10;
                if(mStartAngles1 >= 315){
                    isArc1Add = true;
                }
            }

            if(isArc2Add){
                mStartAngles2 -= 5;
                mSweepAngle2 += 10;
                if(mStartAngles2 <= 270){
                    isArc2Add = false;
                }
            }else{
                mStartAngles2 += 5;
                mSweepAngle2 -= 10;
                if(mStartAngles2 >= 315){
                    isArc2Add = true;
                }
            }

            invalidate();
        }
    };

}
